<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>添加/编辑群组</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="100px" ref="editForm">
          <el-form-item label="群组名称:" prop="name">
            <el-input v-model="editForm.name" auto-complete="off" placeholder="填写群组名称"></el-input>
          </el-form-item>
          <el-form-item label="群组人员：" prop="num">
            <el-button @click.native="getMoreTeach()" size="small">选择群组人员（多选）</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(line)"
                  class="mr-10"
                  v-for="(items,line) in dynamicTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit">确定并保存</el-button>
      </div>
    </div>
    <!--    单选教职工审核人-->
    <MultipleTeacherModle :visible.sync="singleTeachVisible" @studentFn="studentFn"
                          :dynamicTagsArray="examine_user"></MultipleTeacherModle>
  </div>
</template>

<script>
import {groupAdd, groupDetail, groupEdit} from "@/api/admin/group"

export default {
  props: ['visible', 'groupId'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      singleTeachVisible: false,
      dialogVisible: false,
      examine_user: {}, // 审核人
      dynamicTags: [],
      editForm: {
        name: '', // 名称
        user_ids: '' // 审核人
      }, // 编辑界面数据
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.groupId) {
          this.getDetail();
        }
      } else {
        for (let i in this.editForm) {
          this.editForm[i] = '';
        }
      }
    }
  },
  created() {
  },
  methods: {
    // 打开审核人
    getMoreTeach() {
      let a = {}
      for (let item of this.dynamicTags) {
        a[item.id] = item
      }
      this.examine_user = this.getJson(a)
      this.singleTeachVisible = true;
    },
    // 删除审核人
    handleCloseExamine(index) {
      this.dynamicTags.splice(index, 1)
    },
    // 审核人回执
    studentFn(option) {
      let list = []
      for (let i in option) {
        list.push(option[i])
      }
      this.dynamicTags = this.getJson(list)
      console.log('提交的参数：', option, this.dynamicTags)
    },
    getDetail() {
      groupDetail({id: this.groupId}).then(res => {
        console.log('res:', res)
        this.dynamicTags = this.getJson(res.vo.user_ids);
        this.editForm.name = res.vo.name;
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    // 提交数据
    async editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      if (!dataOption.name) {
        this.$message.error('填写群组名称')
        return
      }
      if (!this.dynamicTags.length) {
        this.$message.error('请选择群组人员')
        return
      }
      let a = []
      for (let item of this.dynamicTags) {
        a.push(item.id)
      }
      dataOption.user_ids = a.join(',')
      // name 	整型 	必填 	1 	- 	名称
      // user_ids 	整型 	必填 	1 	- 	用户id多个逗号隔开,
      if (this.groupId) {
        dataOption.id = this.groupId
        groupEdit(dataOption).then(res => {
          if (!res.code) {
            this.$message.success('编辑成功！')
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        groupAdd(dataOption).then(res => {
          if (!res.code) {
            this.$message.success('编辑成功！')
            this.$emit('InitFn')
            this.closePOpup()
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 50%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


